package com.zysdev.openglbeautydemo.filter

import android.content.Context
import android.opengl.*
import android.view.Surface
import java.lang.RuntimeException

/**
 * 创建EGL环境
 */
class EGLEnv {

    private var mEglDisplay: EGLDisplay? = null
    private var mEglContext: EGLContext? = null
    private var mEglConfig: EGLConfig? = null
    private var mEglSurface: EGLSurface? = null

    private var showScreenFilter: ShowScreenFilter? = null

    constructor(
        context: Context,
        eglContext: EGLContext,//GLSurfaceView里的EGLContext，创建的环境可以共享GLSurfaceView的内容
        surface: Surface,
        width: Int,
        height: Int
    ) {
        //获得显示窗口，
        mEglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)

        if (mEglDisplay == EGL14.EGL_NO_DISPLAY) {
            throw RuntimeException("eglGetDisplay failed")
        }


//        100%  固定代码
        // 初始化顯示窗口
        val version = IntArray(2)
        if (!EGL14.eglInitialize(mEglDisplay, version, 0, version, 1)) {
            throw RuntimeException("eglInitialize failed")
        }

        // 配置 属性选项
        val configAttribs = intArrayOf(
            EGL14.EGL_RED_SIZE, 8,  //颜色缓冲区中红色位数
            EGL14.EGL_GREEN_SIZE, 8,  //颜色缓冲区中绿色位数
            EGL14.EGL_BLUE_SIZE, 8,  //
            EGL14.EGL_ALPHA_SIZE, 8,  //
            EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,  //opengl es 2.0
            EGL14.EGL_NONE//表示后面没数据了
        )
        val numConfigs = IntArray(1)
        val configs = arrayOfNulls<EGLConfig>(1)

        //EGL 根据属性选择一个配置
        if (!EGL14.eglChooseConfig(
                mEglDisplay, configAttribs, 0, configs, 0, configs.size,
                numConfigs, 0
            )
        ) {
            throw RuntimeException("EGL error " + EGL14.eglGetError())
        }
        mEglConfig = configs[0]
        val context_attrib_list = intArrayOf(
            EGL14.EGL_CONTEXT_CLIENT_VERSION,//版本是2
            2,
            EGL14.EGL_NONE
        )
        //   使用传进来的共享EGLContext创建好了之后可以读取到数据
        mEglContext =
            EGL14.eglCreateContext(mEglDisplay, mEglConfig, eglContext, context_attrib_list, 0)


        if (mEglContext === EGL14.EGL_NO_CONTEXT) {
            throw RuntimeException("EGL error " + EGL14.eglGetError())
        }

        //        还没完
//        MediaProjection mediaProjection;
//        mediaProjection.createVirtualDisplay()

        /**
         * 创建EGLSurface
         */
        val surface_attrib_list = intArrayOf(
            EGL14.EGL_NONE
        )
//        绑定mediacode的surface
        mEglSurface =
            EGL14.eglCreateWindowSurface(mEglDisplay, mEglConfig, surface, surface_attrib_list, 0)
        // mEglSurface == null
        if (mEglSurface == null) {
            throw RuntimeException("EGL error " + EGL14.eglGetError())
        }
        /**
         * 绑定当前线程的显示器display mEglDisplay  虚拟的物理设备
         */
        if (!EGL14.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
            throw RuntimeException("EGL error " + EGL14.eglGetError())
        }

        showScreenFilter = ShowScreenFilter(context)
        showScreenFilter!!.setSize(width,height)
    }

    fun draw(textureId: Int, timestamp: Long) {
        showScreenFilter?.onDraw(textureId)
        //给帧缓冲   时间戳
        EGLExt.eglPresentationTimeANDROID(mEglDisplay, mEglSurface, timestamp)
        //EGLSurface是双缓冲模式
        EGL14.eglSwapBuffers(mEglDisplay, mEglSurface)
    }


    fun release() {
        EGL14.eglDestroySurface(mEglDisplay, mEglSurface)
        EGL14.eglMakeCurrent(
            mEglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE,
            EGL14.EGL_NO_CONTEXT
        )
        EGL14.eglDestroyContext(mEglDisplay, mEglContext)
        EGL14.eglReleaseThread()
        EGL14.eglTerminate(mEglDisplay)
        showScreenFilter?.release()
        showScreenFilter=null
    }
}